<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/sys-admin' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>添加品牌</el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>

    <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="130px" class="demo-ruleForm">
      <el-form-item label="品牌名称" prop="brandIds">
        <el-select v-model="ruleForm.brandIds"  multiple placeholder="请选择">
          <el-option
              v-for="item in brandListOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              @click.native="addBrand(item.id)"
              >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="系列名称" prop="seriesIds" >
        <el-select v-model="ruleForm.seriesIds"    multiple placeholder="请选择">
          <el-option
              v-for="item in seriesListOptions"
              :key="item.id"
              :label="item.name"
              :value="item.id"
              @click.native="addSeries(item.id)">
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="名称" prop="name">
        <el-input v-model="ruleForm.name"></el-input>
      </el-form-item>
      <el-form-item label="库存" prop="num">
        <el-input v-model="ruleForm.num"></el-input>
      </el-form-item>
      <el-form-item label="原价" prop="oldPrice">
        <el-input v-model="ruleForm.oldPrice"></el-input>
      </el-form-item>
      <el-form-item label="现价" prop="price">
        <el-input v-model="ruleForm.price"></el-input>
      </el-form-item>
      <el-form-item label="手机图片">
        <!--limit限制图片上传的数量-->
        <el-upload
            limit="1"
            action="/upload"
            name="picFile"
            list-type="picture-card"
            :on-preview="handlePictureCardPreview"
            :on-success="handleSuccess"
            :on-remove="handleRemove">
          <i class="el-icon-plus"></i>
        </el-upload>
        <el-dialog :visible.sync="dialogVisible">
          <img width="100%" :src="dialogImageUrl" alt="">
        </el-dialog>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm('ruleForm')">添加</el-button>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    return {
      brandListOptions:[],
      seriesListOptions:[],
      ruleForm: {
        brandIds: [],
        brandId: '',
        seriesId:'',
        name: '',
        num: '',
        oldPrice: '',
        price: '',
        seriesIds: []
      },
      rules: {
        name: [
          {required: true, message: '请输入手机名称', trigger: 'blur'},
          {min: 4, max: 10, message: '长度在 4 到 10 个字符', trigger: 'blur'}
        ],
        brandId: [
          {required: true, message: '请输入品牌id', trigger: 'blur'},
        ],
        seriesId: [
          {required: true, message: '请输入手机系列Id', trigger: 'blur'},
        ],
        num: [
          {required: true, message: '请输入库存数', trigger: 'blur'},
        ],
        oldPrice: [
          {required: true, message: '请输入手机原价', trigger: 'blur'},
        ],
        price: [
          {required: true, message: '请输入手机现价', trigger: 'blur'},
        ]
      }
    };
  },
  methods: {
    addBrand(id){
      // alert("---------------------"+brand)
      this.ruleForm.brandId = id;
       this.loadSeriesList(id);
    },

    addSeries(id){
      console.log(id)
      this.ruleForm.seriesId = id;

    },
    loadBrandList() {
      console.log('准备从服务器端加载【手机品牌列表】……');
      let url = 'http://localhost:9081/brands/brandList';
      console.log('请求路径：' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url)
          .then((response) => {
            let responseBody = response.data;
            console.log('接收到服务器端响应的结果：');
            console.log(responseBody);
            this.brandListOptions = responseBody.data;
            console.log(brandListOptions)
          });
    },
    loadSeriesList(id) {
      let url = 'http://localhost:9081/series/'+id+'/list';
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url) .then((response) => {

            this.seriesListOptions = response.data.data;
          });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let url = 'http://localhost:9081/phones/add-new';
          console.log('尝试添加品牌……');
          console.log('请求路径为：' + url);
          console.log('请求参数为：' + this.ruleForm);
          console.log(this.ruleForm);
          let formData = this.qs.stringify(this.ruleForm);
          console.log('将ruleForm对象转换为FormData：');
          console.log(formData);
          this.axios.post(url, formData).then((response) => {
            console.log('服务器端响应的结果：' + response);
            console.log(response);
            let responseBody = response.data;
            if (responseBody.state == 20000) {
              console.log('添加商品成功');
              this.$message({
                message: '添加商品成功！',
                type: 'success'
              });
            } else {
              console.log(responseBody.message);
              this.$message.error(responseBody.message);
            }
          });
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    }
  },
  mounted() {
    console.log('mounted');
    this.loadBrandList();
  }
}

</script>

<style scoped>

</style>